﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using VietThinks.Contract;

namespace VietThinks.Website
{
    public static partial class VisitContext
    {
        public class ApplicationStateManager
        {
            private ApplicationStateManager() { }

            private static readonly ApplicationStateManager instance = new ApplicationStateManager();

            public static ApplicationStateManager Instance
            {
                get { return instance; }
            }

            public void Clear()
            {
                HttpContext.Current.Application.Clear();
            }

            public IList<VisitorOnlineDto> VisitorOnline
            {
                get
                {
                    return (IList<VisitorOnlineDto>)HttpContext.Current.Application["WhoIsOnline"] ?? new List<VisitorOnlineDto>();
                }
            }

            public string SessionId
            {
                get
                {
                    return HttpContext.Current.Session.SessionID ?? String.Empty;
                }
            }

            //public void AddVisitor(VisitorOnlineDto visitor)
            //{
            //    IList<VisitorOnlineDto> visitors = (IList<VisitorOnlineDto>)HttpContext.Current.Application["WhoIsOnline"] ??
            //                                       new List<VisitorOnlineDto>();

            //    var adminService = ServiceProvider.GetService<IAdminService>();                

            //    var addedVisitor = adminService.AddVisitor(visitor);
            //    visitors.Add(addedVisitor);

            //    HttpContext.Current.Application.Lock();
            //    HttpContext.Current.Application["WhoIsOnline"] = visitors;
            //    HttpContext.Current.Application.UnLock();
            //}

            //public void UpdateVisitor(VisitorOnlineDto visitor)
            //{
            //    IList<VisitorOnlineDto> visitorsOnSession = (IList<VisitorOnlineDto>)HttpContext.Current.Application["WhoIsOnline"] ??
            //                                       new List<VisitorOnlineDto>();

            //    var removedVisitor = visitorsOnSession.FirstOrDefault(v => v.SessionId == visitor.SessionId);
            //    visitorsOnSession.Remove(removedVisitor);

            //    var adminService = ServiceProvider.GetService<IAdminService>();

            //    var updatedVisitor = adminService.UpdateVisitor(visitor);
            //    visitorsOnSession.Add(updatedVisitor);

            //    HttpContext.Current.Application.Lock();
            //    HttpContext.Current.Application["WhoIsOnline"] = visitorsOnSession;
            //    HttpContext.Current.Application.UnLock();

            //}
        }
    }
}